-- 2012-05-17 EM
alter procedure sp_get_user_phones
	@uid int,
	@phones varchar(max) output
as
begin
	set nocount on;
	
	set @phones = '';
	declare @now datetime set @now = getdate();
	declare @phone varchar(20);
	declare cur cursor for
		select phone from dbdata.dbo.phones 
		 where uid = @uid and enabled = 1 and @now <= isnull(dt_removed, @now + 1) 
		 order by as_default desc;
	open cur;
	fetch cur into @phone;
	while @@fetch_status = 0
	begin
		set @phones = @phones + ',' + @phone;
		fetch cur into @phone;
	end;
	close cur;
	deallocate cur;

	-- remove first ","
	if len(@phones) > 0
		set @phones = substring(@phones, 2, 100000);
end;
go

grant execute on sp_get_user_phones to Executer;
